package com.banmao.mall.service.admin.impl;

import com.banmao.mall.mapper.AdMapper;
import com.banmao.mall.mapper.CouponMapper;
import com.banmao.mall.mapper.CouponUserMapper;
import com.banmao.mall.mapper.TopicMapper;
import com.banmao.mall.model.bo.admin.advertising.AdCreatBO;
import com.banmao.mall.model.pojo.Ad;
import com.banmao.mall.model.pojo.Coupon;
import com.banmao.mall.model.pojo.CouponUser;
import com.banmao.mall.model.pojo.Topic;
import com.banmao.mall.model.vo.admin.promote.advertising.AdCreateVO;
import com.banmao.mall.model.vo.admin.promote.advertising.AdItemsBean;
import com.banmao.mall.model.vo.admin.promote.advertising.AdListVO;
import com.banmao.mall.model.vo.admin.promote.coupons.CouponListUserVO;
import com.banmao.mall.model.vo.admin.promote.coupons.CouponListVO;
import com.banmao.mall.model.vo.admin.promote.coupons.CouponItemsBean;
import com.banmao.mall.model.vo.admin.promote.topic.TopicCreateVO;
import com.banmao.mall.model.vo.admin.promote.topic.TopicItemsBean;
import com.banmao.mall.model.vo.admin.promote.topic.TopicListVO;
import com.banmao.mall.service.admin.AdService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.concurrent.Callable;

@Transactional
@Service
public class AdServiceImpl implements AdService {
    @Autowired
    AdMapper adMapper;
    @Autowired
    CouponMapper couponMapper;
    @Autowired
    TopicMapper topicMapper;
    @Autowired
    CouponUserMapper couponUserMapper;
    /**
     * 获取广告列表
     * @param page
     * @param limit
     * @param sort
     * @param order
     * @param name
     * @param content
     * @return
     */
    @Override
    public AdListVO selectAdlist(Integer page, Integer limit, String sort, String order,String name,String content) {
        List<AdItemsBean> beanList = adMapper.selectAdList((page - 1)*limit,limit,sort,order,name,content);
        AdListVO adListVO = new AdListVO();
        adListVO.setItems(beanList);

        Integer total = adMapper.selectAdTotal((page - 1)*limit,limit,sort,order,name,content);
        adListVO.setTotal(total);
        return adListVO;
}

    /**
     * 添加广告
     * @param adCreatBO
     * @return
     */
    @Override
    public AdCreateVO adCreate(AdCreatBO adCreatBO) {
        Ad ad = new Ad();
//        long now = System.currentTimeMillis();
//        long end = now + 60*60*24*30;
//        new Date(end);
//        ad.setStartTime(new Date());
        ad.setAddTime(new Date());
        ad.setName(adCreatBO.getName());
        ad.setLink(adCreatBO.getLink());
        ad.setEnabled(adCreatBO.getEnabled());
        ad.setUrl(adCreatBO.getUrl());
        ad.setPosition( Byte.parseByte(String.valueOf(adCreatBO.getPosition())));
        ad.setContent(adCreatBO.getContent());
        ad.setUpdateTime(new Date());
        ad.setDeleted(false);
        Integer affect = adMapper.insert(ad);
        AdCreateVO adCreateVO = new AdCreateVO();
        if (affect == 1){
            Integer id = ad.getId();
            adCreateVO.setId(id);
            adCreateVO.setName(adCreatBO.getName());
            adCreateVO.setLink(adCreatBO.getLink());
            adCreateVO.setUrl(adCreatBO.getUrl());
            adCreateVO.setPosition(ad.getPosition());
            adCreateVO.setContent(ad.getContent());
            adCreateVO.setEnabled(adCreatBO.getEnabled());
            adCreateVO.setAddTime(ad.getAddTime());
            adCreateVO.setUpdateTime(ad.getUpdateTime());
        }
        return adCreateVO;
    }
    /**
     * 更新广告信息
     */
    @Override
    public int updateAd(Ad ad) {
        ad.setUpdateTime(new Date());
        try {
            int affectedrow = adMapper.updateByPrimaryKeySelective(ad);
            if (affectedrow == 1){
                return 200;
            }else {
                return 404;
            }
        }catch (Exception e){

        }
        return 500;
    }

    /**
     * 根据ID删除广告
     * @param ad
     * @return
     */

    @Override
    public int delete(Ad ad) {
        Integer affectedrow = adMapper.deleteById(ad.getId());

        try {
            if (affectedrow == 1) {
                return 200;
            } else {
                return 404;
            }
        } catch (Exception e) {

        }
        return 500;
    }

    @Override
    public CouponListVO couponList(Integer page, Integer limit, String sort, String order, String name, Integer type, Integer status) {
        List<CouponItemsBean> beanList= couponMapper.selectCouponList((page - 1) * limit,limit,sort,order,name,type,status);
        CouponListVO couponListVO = new CouponListVO();
        couponListVO.setItems(beanList);
        Integer total = couponMapper.selectTotal((page - 1) * limit,limit,sort,order,name,type,status);
        couponListVO.setTotal(total);
        return couponListVO;
    }

    @Override
    public Coupon couponCreate(Coupon coupon) {
        coupon.setAddTime(new Date());
        coupon.setUpdateTime(new Date());
        coupon.setDeleted(false);
        int affectedrow = couponMapper.insert(coupon);
        if (affectedrow == 1){
            coupon.setId(coupon.getId());
        }
        return coupon;
    }

    @Override
    public Coupon couponUpdate(Coupon coupon) {
        couponMapper.updateByPrimaryKey(coupon);
        Coupon coupon1 = couponMapper.selectByPrimaryKey(coupon.getId());
        return coupon1;
    }

    @Override
    public void couponDelete(Coupon coupon) {
        couponMapper.deleteByPrimaryKey(coupon.getId());
    }

    @Override
    public Coupon readDetailById(int id) {
        Coupon coupon = couponMapper.selectByPrimaryKey(id);
        return coupon;
    }

    @Override
    public TopicListVO selectTopicList(Integer page, Integer limit, String sort, String order, String title, String subtitle) {
        List<TopicItemsBean> topicItemsBeans = topicMapper.selectTopicList(page,limit,sort,order,title,subtitle);
        int topic = topicMapper.getTopicListTotal(title,subtitle);
        TopicListVO topicListVO = new TopicListVO();
        topicListVO.setItems(topicItemsBeans);
        topicListVO.setTotal(topic);
        return topicListVO;
    }

    @Override
    public Topic creatTopic(Topic topic) {
        topic.setAddTime(new Date());
        topic.setUpdateTime(new Date());
        topic.setDeleted(false);
        topic.setSortOrder(0);
        int affectedRows = topicMapper.insertSelective(topic);
        Topic topic1 = new Topic();
        if (affectedRows == 1){
            topic1 = topicMapper.selectByPrimaryKey(topic.getId());
        }
        return topic1;
    }

    @Override
    public int updateTopic(Topic topic) {
        try {
            int result = topicMapper.updateByPrimaryKey(topic);
            if (result == 1){
                return 200;
            }else{
                return 404;
            }
        }catch (Exception e){

        }
        return 500;
    }

    @Override
    public int deleteTopic(Topic topic) {
        try {
            int affectedRows = topicMapper.deleteByPrimaryKey(topic.getId());
            if (affectedRows == 1){
                return 200;
            }else {
                return 404;
            }
        }catch (Exception e){

        }
        return 500;
    }

    @Override
    public CouponListUserVO getCouponList(Integer page, Integer limit, Integer couponId, Integer userId, String status, String sort, String order) {
        List<CouponUser> couponUsers = couponUserMapper.getCouponList(page,limit,couponId,userId,status,sort,order);
        CouponListUserVO couponListUserVO = new CouponListUserVO();
        int total = couponUserMapper.getTotal(userId,status);
        couponListUserVO.setTotal(total);
        couponListUserVO.setItems(couponUsers);
        return couponListUserVO;
    }

}
